﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLMS.DAL;
using OLMS.BLL;

namespace OLMS.Admin
{
    public partial class GenresManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenreBinder();
            }
        }

        private void GenreBinder()
        {
            List<Genres> list = GenreBLL.GetGenresList();
            var genresList = list.Select(g => new
            {
                GenreId = g.GenreId,
                Name = g.Name,
                Description = g.Description.Substring(0, 30) + "..."
            }).ToList();
            repGenresList.DataSource = genresList;
            repGenresList.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("GenresView.aspx");
        }

        protected void btnEdit_Command(object sender, CommandEventArgs e)
        {
            string url = string.Format("GenresView.aspx?gid={0}", e.CommandArgument);
            Response.Redirect(url);
        }

        protected void btnRemove_Command(object sender, CommandEventArgs e)
        {
            int genreId = Convert.ToInt32(e.CommandArgument);
            if (GenreBLL.RemoveGenre(genreId))
            {
                GenreBinder();
            }
        }
    }
}